interface fitCanvas {
  width: number;
  height: number;
  zoom: string;
}
function autoFit(cw: number, ch: number, bw: number, bh: number): fitCanvas {
  const ratio = 0.9;

  const result: fitCanvas = {
    width: cw,
    height: ch,
    zoom: "100.0%",
  };

  if (cw <= bw * ratio && ch <= bh * ratio) {
    return result;
  } else {
    const scaleW = (bw * ratio) / cw;
    const scaleH = (bh * ratio) / ch;

    const scale = scaleW <= scaleH ? scaleW : scaleH;

    return {
      width: Number.parseInt((cw * scale).toFixed(0)),
      height: Number.parseInt((ch * scale).toFixed(0)),
      zoom: (100 * Number.parseFloat(scale.toFixed(3))).toString() + "%",
    };
  }
}

export default autoFit;
